package com.james.happyfarm.servlets;

import java.io.IOException;
import java.util.Calendar;
import java.util.TimeZone;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.james.happyfarm.JDO.Message;
import com.james.happyfarm.JDO.PMF;
import com.james.happyfarm.PO.Farm;
import com.james.happyfarm.PO.Friend;
import com.james.happyfarm.modules.GardenModule;
import com.james.happyfarm.network.KaiXinWap;

public class GardenSpy extends HttpServlet {
	
	private static final long serialVersionUID = -3427382885295764399L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		int hour = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00")).get(Calendar.HOUR_OF_DAY);
		if (hour >= 2 && hour < 6)
			return;
		
		String verifyCode = KaiXinWap.ME_VERIFYCODE;
		GardenModule gm = new GardenModule();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		for (Friend friend : gm.getStealableFriend(verifyCode)) {
			for (Farm farm : gm.getStealableFarm(friend.getGarden(), verifyCode).getFarmList()) {
				pm.makePersistent(new Message(friend.getUserName(), gm.stealFarm(farm, verifyCode)));
			}
		}
	}

}
